﻿using System;
using AirIndex.Models.HtmlClient.SendData;
using HVACWebService.Cloud.WeChat;
using HVACWebService.Common;
using HVACWebService.DataAccese.DataService;
using HVACWebService.DataAccese.Entity;

namespace HVACWebService.Business
{
    public class WeChatTokenService
    {
        /// <summary>
        /// 获取微信JsApi Tokens
        /// </summary>
        /// <param name="soureUrl"></param>
        /// <returns></returns>
        public Tokens GetJsApiTokens(string soureUrl)
        {
            var accessToken = GetWeiXinAccessToken(true);
            var jsapi = GetWeiXinJsApiTicket(true, accessToken);

            var weiXinToken = new WeiXinToken();
            var acc = weiXinToken.GetTokens(accessToken, jsapi, soureUrl);
            return acc;
        }

        /// <summary>
        ///  获取微信服务器端Access
        /// </summary>
        /// <returns></returns>
        public string GetWeiXinAccessToken(bool isCheckTime = true)
        {
            var weChatTokenExpire = int.Parse(ConfigHelper.GetValue<string>("WeChatTokenExpire"));
            var weChatToken = "";
            var weChatTokenInfo = GeWeChatToken("ACC");
            var dateTimeNow = DateTime.Now;
            if (isCheckTime)
            {
                if (weChatTokenInfo?.LastUpDate != null && (weChatTokenInfo.LastUpDate.Value. AddMinutes(weChatTokenExpire) >= dateTimeNow))
                {
                    weChatToken = weChatTokenInfo.TokenValue;
                }
                else
                {
                    var weiXinToken = new WeiXinToken();
                    weChatToken = weiXinToken.GetAccessToken();
                    UpdateToken("ACC", weChatToken);
                }
            }
            else
            {
                var weiXinToken = new WeiXinToken();
                weChatToken = weiXinToken.GetAccessToken();
                UpdateToken("ACC", weChatToken);
            }



            return weChatToken;
        }


        public string GetWeiXinJsApiTicket(bool isCheckTime, string acc)
        {
            var weChatTokenExpire = int.Parse(ConfigHelper.GetValue<string>("WeChatTokenExpire"));
            var weChatToken = "";
            var weChatTokenInfo = GeWeChatToken("JS");
            var dateTimeNow = DateTime.Now;

            if (isCheckTime)
            {
                if (weChatTokenInfo?.LastUpDate != null && (weChatTokenInfo.LastUpDate.Value.AddMinutes(weChatTokenExpire) >= dateTimeNow))
                {
                    weChatToken = weChatTokenInfo.TokenValue;
                }
                else
                {
                    var weiXinToken = new WeiXinToken();
                    weChatToken = weiXinToken.GetJsApiTicket(acc);
                    UpdateToken("JS", weChatToken);
                }
            }
            else
            {
                var weiXinToken = new WeiXinToken();
                weChatToken = weiXinToken.GetJsApiTicket(acc);
                UpdateToken("JS", weChatToken);
            }

            return weChatToken;
        }


        /// <summary>
        /// 更新Token
        /// </summary>
        /// <param name="key"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public int UpdateToken(string key, string token)
        {

            WeChatTokenDataAccess weChatTokenDataAccess = new WeChatTokenDataAccess();
            return weChatTokenDataAccess.UpdateToken(key, token);
        }

        /// <summary>
        /// 获取Token
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public WeChatToken GeWeChatToken(string key)
        {
            WeChatTokenDataAccess weChatTokenDataAccess = new WeChatTokenDataAccess();
            return weChatTokenDataAccess.GeWeChatToken(key);
        }
    }
}